package cn.edu.sgu.www.mhxysy.controller;

import cn.edu.sgu.www.mhxysy.base.Pager;
import cn.edu.sgu.www.mhxysy.consts.MimeType;
import cn.edu.sgu.www.mhxysy.dto.RoleAccountDTO;
import cn.edu.sgu.www.mhxysy.entity.RoleAccount;
import cn.edu.sgu.www.mhxysy.restful.JsonPage;
import cn.edu.sgu.www.mhxysy.restful.JsonResult;
import cn.edu.sgu.www.mhxysy.service.RoleAccountService;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author heyunlin
 * @version 1.0
 */
@RestController
@RequestMapping(path = "/role_account", produces = MimeType.APPLICATION_JSON_CHARSET_UTF_8)
public class RoleAccountController {

	private final RoleAccountService roleAccountService;

	@Autowired
	public RoleAccountController(RoleAccountService roleAccountService) {
		this.roleAccountService = roleAccountService;
	}

	@RequestMapping(value = "/insert", method = RequestMethod.POST)
	public JsonResult<Void> insert(@Validated RoleAccountDTO roleAccountDTO) {
		roleAccountService.insert(roleAccountDTO);

		return JsonResult.success("添加成功");
	}

	@RequestMapping(value = "/deleteById", method = RequestMethod.POST)
	public JsonResult<Void> deleteById(@RequestParam("id") String roleId) {
		roleAccountService.deleteById(roleId);

		return JsonResult.success("删除成功");
	}

	@RequestMapping(value = "/deleteByIds", method = RequestMethod.POST)
	public JsonResult<Void> deleteByIds(@RequestParam("ids") List<String> ids) {
		roleAccountService.deleteByIds(ids);

		return JsonResult.success("删除成功");
	}

	@RequestMapping(value = "/updateById", method = RequestMethod.POST)
	public JsonResult<Void> updateById(@Validated RoleAccountDTO roleAccountDTO) {
		roleAccountService.updateById(roleAccountDTO);

		return JsonResult.success("修改成功");
	}

	@RequestMapping(value = "/selectAll", method = RequestMethod.GET)
	public JsonResult<List<RoleAccount>> selectAll() {
		List<RoleAccount> list = roleAccountService.selectAll();

		return JsonResult.success(null, list);
	}

	@RequestMapping(value = "/selectById", method = RequestMethod.GET)
	public JsonResult<RoleAccount> selectById(@RequestParam("id") String roleId) {
		RoleAccount roleAccount = roleAccountService.selectById(roleId);

		return JsonResult.success(null, roleAccount);
	}

	@RequestMapping(value = "/selectByPage", method = RequestMethod.POST)
	public JsonResult<JsonPage<RoleAccount>> selectByPage(Pager<RoleAccount> pager) {
		Page<RoleAccount> page = roleAccountService.selectByPage(pager);

		return JsonResult.restPage(page);
	}

}